-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Added Median of Medians Algorithm #9864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
for more information, see https://pre-commit.ci
searches/median_of_medians.py
Outdated
|
||
""" | ||
>>> quick_select([2, 4, 5, 7, 899, 54, 32], 5) | ||
32 | ||
>>> quick_select([2, 4, 5, 7, 899, 54, 32], 1) | ||
2 | ||
>>> quick_select([5, 4, 3, 2], 2) | ||
3 | ||
>>> quick_select([3, 5, 7, 10, 2, 12], 3) | ||
5 | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
""" | |
>>> quick_select([2, 4, 5, 7, 899, 54, 32], 5) | |
32 | |
>>> quick_select([2, 4, 5, 7, 899, 54, 32], 1) | |
2 | |
>>> quick_select([5, 4, 3, 2], 2) | |
3 | |
>>> quick_select([3, 5, 7, 10, 2, 12], 3) | |
5 | |
""" |
There is no point in repeating the same tests here.
searches/median_of_medians.py
Outdated
""" | ||
>>> median_of_five([2, 4, 5, 7, 899]) | ||
5 | ||
>>> median_of_five([5, 7, 899, 54, 32]) | ||
32 | ||
>>> median_of_five([5, 4, 3, 2]) | ||
3 | ||
>>> median_of_five([3, 5, 7, 10, 2]) | ||
5 | ||
""" | ||
""" | ||
Return the median of the input list | ||
:param arr: Array to find median of | ||
:return: median of arr | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you swap the order of the tests and explanation, and condense into a single docstring
searches/median_of_medians.py
Outdated
""" | ||
>>> median_of_medians([2, 4, 5, 7, 899, 54, 32]) | ||
54 | ||
>>> median_of_medians([5, 7, 899, 54, 32]) | ||
32 | ||
>>> median_of_medians([5, 4, 3, 2]) | ||
4 | ||
>>> median_of_medians([3, 5, 7, 10, 2, 12]) | ||
12 | ||
""" | ||
""" | ||
Return a pivot to partition data on by calculating | ||
Median of medians of input data | ||
:param arr: The data to be checked (a list) | ||
:return: median of medians of input array | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you swap the order of the tests and explanation, and condense into a single docstring
searches/median_of_medians.py
Outdated
""" | ||
>>> quick_select([2, 4, 5, 7, 899, 54, 32], 5) | ||
32 | ||
>>> quick_select([2, 4, 5, 7, 899, 54, 32], 1) | ||
2 | ||
>>> quick_select([5, 4, 3, 2], 2) | ||
3 | ||
>>> quick_select([3, 5, 7, 10, 2, 12], 3) | ||
5 | ||
""" | ||
|
||
""" | ||
Two way partition the data into smaller and greater lists, | ||
in relationship to the pivot | ||
:param arr: The data to be searched (a list) | ||
:param target: The rank to be searched | ||
:return: element at rank target | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you swap the order of the tests and explanation, and condense into a single docstring
searches/median_of_medians.py
Outdated
>>> median_of_five([5, 4, 3, 2]) | ||
3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is failing:
>>> median_of_five([5, 4, 3, 2])
Expected:
3
Got:
4
* Added Median of Medians Algorithm * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update median_of_medians.py as per requested changes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Describe your change:
Added an Algorithm: Added Median of medians algorithm to find element at rank k in time complexity O(n)
Checklist: